<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Pembagian_Kelas extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_kelas';

		$this->primary_key = 'aka_kelas.kelas_id';

	}
	function get_pembagian_kelas($tahun_ajaran_id,$tingkat_id,$jurusan_id,$aktif=null){
		$this->load->database();	
			$filter = "";
			
			if($jurusan_id == 0){
				$filter = "AND {$this->table_name}.jurusan_id IS NULL";
			}else{
				$filter = "AND {$this->table_name}.jurusan_id='$jurusan_id'";
			}
				
			if($aktif != null)
				$filter .= " AND {$this->table_name}.aktif='$aktif'";
				
			$sql = "SELECT aka_kelas.*,aka_tahun_ajaran.tahun_ajaran,mst_pegawai.nama,aka_tingkat.tingkat FROM aka_kelas JOIN aka_tahun_ajaran ON aka_kelas.tahun_ajaran_id=aka_tahun_ajaran.tahun_ajaran_id JOIN mst_pegawai ON aka_kelas.wali_kelas=mst_pegawai.pegawai_id JOIN aka_tingkat ON aka_kelas.tingkat_id=aka_tingkat.tingkat_id WHERE {$this->table_name}.tahun_ajaran_id='$tahun_ajaran_id' AND {$this->table_name}.tingkat_id='$tingkat_id' $filter ORDER BY aka_kelas.kelas_id";
			
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_all($tingkat_id){
		$this->load->database();	
			
				
			$sql = "SELECT * FROM aka_kelas WHERE tingkat_id='$tingkat_id';";
			
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	function get_by(){
		$this->load->database();		
		$sql = "SELECT * FROM aka_kelas;";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_pembagian_kelas_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM aka_kelas WHERE kelas_id='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				if($data['jurusan_id'] == 0)
					$data['jurusan_id'] = NULL;
				$this->db->insert('aka_kelas',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				if($data['jurusan_id'] == 0)
					$data['jurusan_id'] = NULL;
				$this->db->where('kelas_id',$id);
				$this->db->update('aka_kelas',$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM aka_kelas WHERE kelas_id='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>